//
//  ShareViewController.m
//  MMLOOK
//
//  Created by dllo on 15/8/29.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "ShareViewController.h"
#import "FlowTableViewCell.h"
#import "ShareCVCell.h"
#import "ShareDetaViewController.h"
#import "AsynConnect.h"
#import "AnotherView.h"
#import "AnotherdetaViewController.h"
#import "SaveTools.h"
#import "MBProgressHUD.h"

@interface ShareViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate, AnotherViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, ShareCVCellDelegate>

@property (nonatomic, retain) UISegmentedControl *segmentedControl;
@property (nonatomic, retain) UIScrollView *scrol;
@property (nonatomic, retain) UITableView *tableViewLeft;
@property (nonatomic,retain) NSMutableArray *leftTitleArray;
@property (nonatomic, retain) UIScrollView *RightScrol;
@property (nonatomic, retain) NSMutableArray *webArray;// 储存网址
@property (nonatomic, retain) NSMutableArray *collArray;// 储存数据
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic ,retain) MBProgressHUD *mbProView;

@end

@implementation ShareViewController

- (void)dealloc
{
    [_collArray release];
    [_flowLayout release];
    [_collectionView release];
    [_webArray release];
    [_leftTitleArray release];
    [_RightScrol release];
    [_tableViewLeft release];
    [_scrol release];
    [_segmentedControl release];
    [_mbProView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.leftTitleArray = [NSMutableArray arrayWithObjects:@"女王养成记", @"创意生活", @"千言万语", @"美图分享", @"爱情有天意", @"恋爱语录", @"创意婚纱照", @"古月", @"玫提斯漫画", @"同性恋", @"大牌彩妆", @"致闺蜜", nil];
        
        self.webArray = [NSMutableArray arrayWithObjects:@"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1383743776&user_id=200168717&album_id=4502380075&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1387612759&user_id=1029282466&album_id=4501598257&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1370777946&user_id=370830666&album_id=5443690&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1369788201&user_id=849769295&album_id=22555894&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1369455879&user_id=1068631916&album_id=22905426&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1383653721&user_id=383008379&album_id=22835425&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1396488109&user_id=1711937226&album_id=4501309428&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1383654792&user_id=1060108468&album_id=4500817634&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1391765739&user_id=699029657&album_id=3000866454&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1374561983&user_id=1649855582&album_id=4500454417&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1375164048&user_id=88211911&album_id=20660485&page=1&type=0", @"http://guang.isee.com.cn/homepage/ajax/get_album_source.json?current_page=1&cursor=&t=1384282477&user_id=1057167053&album_id=22747643&page=1&type=0", nil];
        
        self.collArray = [NSMutableArray array];
        
        for (int i = 0; i < self.webArray.count; i++) {
            
            NSMutableArray *array = [NSMutableArray array];
            
            [self.collArray addObject:array];
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBarController.tabBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:253 / 255.0 green:180/255.0 blue:83/255.0 alpha:1];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
    
    [self creatHeaderbar];
    [self creatTable];
    [self creatRightScrolView];
    [self createCollectionView];
    [self firstCell];
}

#warning 默认选中第一个tableView的cell
- (void)firstCell
{
    NSIndexPath *selectIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [self.tableViewLeft selectRowAtIndexPath:selectIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.tabBarController.tabBar.translucent = YES;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

#pragma mark 创建collectionView
- (void)createCollectionView
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.itemSize = CGSizeMake(self.RightScrol.frame.size.width, self.RightScrol.frame.size.height);
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;// 横向滑动
    _flowLayout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.RightScrol.frame.size.width, self.RightScrol.frame.size.height) collectionViewLayout:_flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.showsHorizontalScrollIndicator = NO;// 关闭滚动条
    _collectionView.scrollEnabled = NO;
    [_collectionView registerClass:[ShareCVCell class] forCellWithReuseIdentifier:@"kCellReuseIdentifier"];// 注册cell
    [self.RightScrol addSubview:_collectionView];
    [_collectionView release];
    [_flowLayout release];
}

#pragma mark 实现collectionView协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _leftTitleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShareCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kCellReuseIdentifier" forIndexPath:indexPath];
    
    cell.delegate = self;
    cell.num = indexPath.row;
    cell.arr = [self.collArray objectAtIndex:indexPath.row];
    
    [cell sendDicWithBlock:^(NSMutableDictionary *dic) {
        
        ShareDetaViewController *shareDeta = [[[ShareDetaViewController alloc] init] autorelease];
        shareDeta.detaDic = dic;
        [self.navigationController pushViewController:shareDeta animated:YES];
    }];
    
    return cell;
}

#pragma mark -
#pragma mark 网络请求
- (void)beginRefresh:(ShareCVCell *)cell
{
    NSString *urlStr = [self.webArray objectAtIndex:cell.num];
    
    NSMutableArray *array = [self.collArray objectAtIndex:cell.num];
    
    [self createMbProView];
    
    [AsynConnect asynConnectWithUrlstr:urlStr parmater:nil block:^(NSData *data) {
        
        BOOL saveResult = [SaveTools saveDataWithName:[urlStr hash] directory:NSLibraryDirectory data:data];// 有网络时 数据储存在本地
        NSLog(@"点你所想储存：%d", saveResult);
        
        [array removeAllObjects];
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *littledic = [dic objectForKey:@"data"];
        NSMutableArray *arr = [littledic objectForKey:@"data"];
        
        [array addObjectsFromArray:arr];
        
        cell.arr = [_collArray objectAtIndex:cell.num];
        
        [_mbProView hide:YES];
    }];
}

- (void)readLocalData:(ShareCVCell *)cell
{
    NSString *urlStr = [self.webArray objectAtIndex:cell.num];
    
    NSMutableArray *array = [self.collArray objectAtIndex:cell.num];
    
    NSData *data = [SaveTools readDataWithName:[urlStr hash] directory:NSLibraryDirectory];
    
    if (data) {
        
        [array removeAllObjects];
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *littledic = [dic objectForKey:@"data"];
        NSMutableArray *arr = [littledic objectForKey:@"data"];
        
        [array addObjectsFromArray:arr];
        
        cell.arr = [_collArray objectAtIndex:cell.num];
    }
}

- (void)creatHeaderbar
{
    NSArray *array = @[@"点你所想", @"题外话"];
    
    self.segmentedControl = [[[UISegmentedControl alloc] initWithItems:array] autorelease];
    _segmentedControl.tintColor = [UIColor blackColor];
    self.navigationItem.titleView = _segmentedControl;
    _segmentedControl.selectedSegmentIndex = 0;
    [_segmentedControl addTarget:self action:@selector(tapClick:) forControlEvents:UIControlEventValueChanged];
    
    self.scrol = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)] autorelease];
    _scrol.pagingEnabled = YES;
    _scrol.showsHorizontalScrollIndicator = NO;
    _scrol.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    _scrol.delegate = self;
    _scrol.contentOffset = CGPointMake(0, 0);
    [self.view addSubview:_scrol];
    
    [self creatAnother];
}

//题外话
- (void)creatAnother
{
    AnotherView *therview = [[[AnotherView alloc]initWithFrame:CGRectMake(self.scrol.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height - 64)] autorelease];
    therview.backgroundColor = [UIColor whiteColor];
    therview.delegate = self;
    [self.scrol addSubview:therview];
}

- (void)Anortherjump:(NSMutableDictionary *)dic
{
    AnotherdetaViewController *anotherdeta = [[[AnotherdetaViewController alloc] init] autorelease];
    anotherdeta.dicc = dic;
    [self.navigationController pushViewController:anotherdeta animated:YES];
}

- (void)tapClick: (UISegmentedControl *)seg
{
    self.scrol.contentOffset = CGPointMake(seg.selectedSegmentIndex * self.scrol.frame.size.width, 0);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageW = self.view.frame.size.width;
    NSInteger page = _scrol.contentOffset.x / pageW;
    [self.segmentedControl setSelectedSegmentIndex:page];
}

- (void)creatRightScrolView
{
    self.RightScrol = [[[UIScrollView alloc] initWithFrame:CGRectMake(90 * OffWidth, 0, 285 * OffWidth, self.scrol.frame.size.height)] autorelease];
    UIImageView *image = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.RightScrol.frame.size.width, self.RightScrol.frame.size.height)] autorelease];
    image.image = [UIImage imageNamed:@"RightScroll.png"];
    [_RightScrol addSubview:image];
    [self.scrol addSubview:_RightScrol];
}

- (void)creatTable
{
    self.tableViewLeft = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 90 * OffWidth, self.view.frame.size.height - 64) style:UITableViewStylePlain] autorelease];
    _tableViewLeft.scrollEnabled = NO;
    _tableViewLeft.delegate = self;
    _tableViewLeft.dataSource = self;
    _tableViewLeft.rowHeight = (self.view.frame.size.height - 64) / _leftTitleArray.count;
    [self.scrol addSubview:_tableViewLeft];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _leftTitleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentity = @"a";
    
    FlowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity];
    
    if (!cell) {
        
        cell = [[[FlowTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity] autorelease];
    }
    
    cell.lable.text = [_leftTitleArray objectAtIndex:indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.collectionView setContentOffset:CGPointMake(indexPath.row * self.RightScrol.frame.size.width, 0)];
}

- (void)createMbProView
{
    self.mbProView = [[[MBProgressHUD alloc] initWithView:self.collectionView] autorelease];
    _mbProView.mode = MBProgressHUDModeIndeterminate;// 样式 菊花
    _mbProView.labelText = @"加载中. . .";
    [_mbProView show:YES];
    [self.collectionView addSubview:_mbProView];
    [_mbProView hide:YES afterDelay:20];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
